package com.nchu.romantic.surprise.service.impl;

import com.nchu.romantic.surprise.model.BaseEnity;
import com.nchu.romantic.surprise.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.Date;
import java.util.Map;

/**
 * @author fujianjian
 * @project react-learning
 * @date 2018/10/21 下午1:18
 */
public abstract class BaseServiceImpl<T extends BaseEnity> implements BaseService<T> {

    @Autowired
    private MongoRepository<T, Integer> repository;

    @Override
    public T get(Integer id) {
        return repository.findById(id).get();
    }

    @Override
    public T create(T record) {
        record.setCreatedTime(new Date());
        record.setModifiedTime(record.getCreatedTime());
        record.setDeletedFlag(false);
        repository.save(record);
        return record;

    }

    @Override
    public T update(T record) {
        record.setModifiedTime(new Date());
        repository.save(record);
        return record;
    }

    @Override
    public void delete(Integer id) {

        T record = get(id);
        record.setDeletedFlag(true);
        update(record);

    }

    @Override
    public Page<T> selectPage(Map param, Pageable pageable) {
        return null;
    }
}
